package com.ny.controller;

import com.ny.entity.Classrooms;
import com.ny.entity.Reservations;
import com.ny.entity.Users;
import com.ny.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class ReservationController {
    @Autowired
    private ReservationService reservationService;
    @RequestMapping("/reservations")
    public String  reservation(Model model){
        model.addAttribute("reservationList", reservationService.findAllReservations());
        return "reservations";
    }


    // 用户查询自己的预约记录
    @GetMapping("/reservations1")
    public String getUserReservations(Model model, HttpSession session) {
        Users user = (Users) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
            List<Reservations> reservations = reservationService.findUserReservations(user.getUserId());
            model.addAttribute("reservationList", reservations);
            return "myReservations";
    }

    @GetMapping("/searchReservations")
    public String getReservations(@RequestParam(required = false) String username, @RequestParam(required = false) String classroomName, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") Date startDatetime, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") Date endDatetime, @RequestParam(required = false) String status, Model model) {
        List<Reservations> reservations = reservationService.findReservations(username, classroomName, startDatetime, endDatetime, status);
        model.addAttribute("reservationList", reservations);
        return "reservations";
    }
}
